<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <head>
    <title>Options - FilePremier - Plugin</title>
    <LINK rel="shortcut icon" href="../images/favicon.ico">
    <LINK href="./tab_style.css" rel="stylesheet" type="text/css">
    <script type="text/javascript">
    document.title = chrome.i18n.getMessage("pref_title_text");

    var tabLinks = new Array();
    var contentDivs = new Array();
    var neverReconnected = true;
    var connInfoChanged = false;

    function setItem(key, value) {
        try {
            window.localStorage.removeItem(key);
            window.localStorage.setItem(key, value);
        }catch(e) {
            console.log(e);
        }
    }

    function getItem(key) {
        var value;
        try {
            value = window.localStorage.getItem(key);
        }catch(e) {
            console.log(e);
            value = "null";
        }
        return value;
    }

    function settingChangedEvt() {
        document.getElementById("file-save-button").disabled = false;
    }

    function loadConfig() {
        document.getElementById("username").value = username = getItem("username");
        document.getElementById("passname").value = password = getItem("password");
        document.getElementById("reconnect-checkbox").checked = (getItem("autoreconnect") == "true");
        document.getElementById("reconnect-option").style.display = (document.getElementById("reconnect-checkbox").checked) ? 'block' : 'none';
        document.getElementById("popup-checkbox").checked = (getItem("showpopup") == "true");
        document.getElementById("popup-pos-div").style.display = (document.getElementById("popup-checkbox").checked) ? 'block' : 'none';
        document.getElementById("popup-pos").value = getItem("popuppos");
        document.getElementById("autodown-checkbox").checked = (getItem("autodownload") != "-1");
        document.getElementById("autodown-value").value = (getItem("autodownload") == "-1") ? 0 : getItem("autodownload");
        document.getElementById("autodown-val-checkbox").style.display = (document.getElementById("autodown-checkbox").checked) ? 'block' : 'none';

        document.getElementById("file-save-button").value = chrome.i18n.getMessage("pref_apply_text");
        document.getElementById("file-cancel-button").value = chrome.i18n.getMessage("pref_cancel_text");
        document.getElementById("file-save-button").disabled = true;
		
        getAccInfo();
    }

    function saveConfig() {
        setItem("username",document.getElementById("username").value);
        setItem("password",document.getElementById("passname").value);
        setItem("autoreconnect", document.getElementById("reconnect-checkbox").checked);
        setItem("showpopup", document.getElementById("popup-checkbox").checked);
        setItem("popuppos", document.getElementById("popup-pos").value);
        setItem("autodownload", (document.getElementById("autodown-checkbox").checked) ? document.getElementById("autodown-value").value : "-1" );
        document.getElementById("file-save-button").disabled = true;
        chrome.extension.sendRequest({'action' : 'reloadSettings'});

        if(connInfoChanged) { 
            chrome.extension.sendRequest({'action' : 'reConnect'}, autoReconnectCallback);
        }
        connInfoChanged = false;
    }


    function initTabs() {

      var tabListItems = document.getElementById('tabs').childNodes;
      for ( var i = 0; i < tabListItems.length; i++ ) {
        if ( tabListItems[i].nodeName == "LI" ) {
          var tabLink = getFirstChildWithTagName( tabListItems[i], 'A' );
          var id = getHash( tabLink.getAttribute('href') );
          tabLinks[id] = tabLink;
          contentDivs[id] = document.getElementById( id );
        }
      }

      var i = 0;

      for ( var id in tabLinks ) {
        tabLinks[id].onclick = showTab;
        tabLinks[id].onfocus = function() { this.blur() };
        if ( i == 0 ) tabLinks[id].className = 'selected';
        i++;
      }

      var i = 0;

      for ( var id in contentDivs ) {
        if ( i != 0 ) contentDivs[id].className = 'tabContent hide';
        i++;
      }
    }

    function showTab() {
      var selectedId = getHash( this.getAttribute('href') );
	  
      for ( var id in contentDivs ) {
        if ( id == selectedId ) {
          tabLinks[id].className = 'selected';
          contentDivs[id].className = 'tabContent';
        } else {
          tabLinks[id].className = '';
          contentDivs[id].className = 'tabContent hide';
        }
      }
      return false;
    }

    function getFirstChildWithTagName( element, tagName ) {
      for ( var i = 0; i < element.childNodes.length; i++ ) {
        if ( element.childNodes[i].nodeName == tagName ) return element.childNodes[i];
      }
    }

    function getHash( url ) {
      var hashPos = url.lastIndexOf ( '#' );
      return url.substring( hashPos + 1 );
    }

    function getAccInfo() {
        document.getElementById('nowloading').style.display = 'block';
        document.getElementById('nowloading').src = "../images/loading.gif";
        document.getElementById('info0').innerHTML = "";
        if(neverReconnected) document.getElementById('info1').innerHTML = "-";
        
         var xmlra = new XMLHttpRequest();
         if(getItem("username") != '') { xmlra.open("GET", "http://www.filepremier.com/fpapi/check.php?login="+getItem("username")+"&pass="+getItem("password"), true); }
		 else { xmlra.open("GET", "http://www.filepremier.com/fpapi/check.php", true); }
         xmlra.send("");
         xmlra.onreadystatechange = function (aEvt) { 
            if ((xmlra.readyState == 4) && (xmlra.status == 200)){
                document.getElementById('nowloading').style.display = 'none';
    
                var connectedUser = xmlra.responseText.match(/<username>[^><]+/);
                if(connectedUser) {
                    document.getElementById('info0').innerHTML = escapeHTML(connectedUser[0].substring(10));
                    document.getElementById('info1').innerHTML = escapeHTML(xmlra.responseText.match(/<expiration-txt>[^><]+/)[0].substring(16));
                } else 
                if((getItem("autoreconnect") == "true") && neverReconnected) {
                    document.getElementById('info0').innerHTML = "Automatic connection ...";
                    chrome.extension.sendRequest({'action' : 'reConnect'}, autoReconnectCallback);

                } else document.getElementById('info0').innerHTML = chrome.i18n.getMessage("not_logged_in");
            }
        };
    }

    function autoReconnectCallback(saywhat) {
        neverReconnected = false;
        if(saywhat) {
            getAccInfo();
            return;
        }
        document.getElementById('info0').innerHTML = chrome.i18n.getMessage("popup_not_connected_text");
        document.getElementById('info1').innerHTML = chrome.i18n.getMessage("popup_error_userpass_text");
    }

    function escapeHTML (str) {
       var div = document.createElement('div');
       var text = document.createTextNode(str);
       div.appendChild(text);
       return div.innerHTML;
    };

    </script>
  </head>

  <body onload="initTabs();loadConfig();">

    <ul id="tabs">
      <li><a href="#myspace"><img src="space.png"><br/><script>document.write(chrome.i18n.getMessage("pref_myspace_text"));</script></a></li>
      <li><a href="#settings"><img src="settings.png"><br/><script>document.write(chrome.i18n.getMessage("pref_general_text"));</script></a></li>
      <li><a href="#about"><img src="about.png"><br/><script>document.write(chrome.i18n.getMessage("pref_about_text"));</script></a></li>
    </ul>

    <div class="tabContent" id="myspace">
      <div id="perspace" class="filecontainer" style="-webkit-user-select:auto;">
        <p class="titre" style="width:29ex;"><script>document.write(chrome.i18n.getMessage("pref_welcome_text"));</script></p>
        <br />
        <table style="margin-top:-20px;margin-bottom:10px;">
            <tr>
                <td class="info_titre"><script>document.write(chrome.i18n.getMessage("pref_connected_as_text"));</script></td>
                <td id="info0" class="info_valeur"></td>
                <td><img id="nowloading" src="../images/loading.gif"></td>
            </tr>
            <tr>
                <td class="info_titre"><script>document.write(chrome.i18n.getMessage("pref_premium_text"));</script></td>
                <td id="info1" class="info_valeur">-</td>
            </tr>
        </table>
      </div>
      <div id="securite" class="filecontainer">
        <p class="titre" style="width:8ex;"><script>document.write(chrome.i18n.getMessage("pref_security_text"));</script></p>

        <input type="checkbox" id="reconnect-checkbox" onclick="document.getElementById('reconnect-option').style.display = (this.checked) ? 'block' : 'none'; settingChangedEvt();" /><label for="reconnect-checkbox">&nbsp;<script>document.write(chrome.i18n.getMessage("pref_autoconnection_text"));</script></label>
        <table id="reconnect-option" style="display:none;">
        <tr><td><script>document.write(chrome.i18n.getMessage("pref_user_text"));</script></td><td><input id="username" maxlength="32" size="32" onchange="settingChangedEvt();connInfoChanged = true;"/><td></tr>
        <tr>
            <td><script>document.write(chrome.i18n.getMessage("pref_password_text"));</script></td>
            <td><input id="passname" type="password"  onchange="settingChangedEvt();connInfoChanged = true;"/><input type="checkbox" onclick='document.getElementById("passname").setAttribute("type",(this.checked) ? "" : "password");' id="showpass-checkbox" /><label for="showpass-checkbox">&nbsp;<script>document.write(chrome.i18n.getMessage("pref_pass_show_text"));</script></label><td>
        </tr>
        </table>
      </div>
    </div>

    <div class="tabContent" id="settings">
      <div id="pref-popup" class="filecontainer">
        <p class="titre" style="width:15ex;"><script>document.write(chrome.i18n.getMessage("pref_popup_text"));</script></p>
        <input type="checkbox" id="popup-checkbox" onclick="document.getElementById('popup-pos-div').style.display = (this.checked) ? 'block' : 'none'; settingChangedEvt();"/><label for="popup-checkbox">&nbsp;<script>document.write(chrome.i18n.getMessage("pref_show_popup_text"));</script></label>
        <br>
        <div id="popup-pos-div" style="margin-top:10px;margin-left:20px;"><script>document.write(chrome.i18n.getMessage("pref_popup_position_text"));</script>
            <select id="popup-pos" onchange="settingChangedEvt();">
                <option value="2"><script>document.write(chrome.i18n.getMessage("pref_popup_left_text"));</script></option>
                <option value="1"><script>document.write(chrome.i18n.getMessage("pref_popup_right_text"));</script></option>
            </select>
        </div>
      </div>

      <div id="tel-auto" class="filecontainer">
        <p class="titre" style="width:28ex;"><script>document.write(chrome.i18n.getMessage("pref_autodownload_text"));</script></p>
        <input type="checkbox" id="autodown-checkbox" onclick="document.getElementById('autodown-val-checkbox').style.display = (this.checked) ? 'block' : 'none'; settingChangedEvt();"/><label for="autodown-checkbox">&nbsp;<script>document.write(chrome.i18n.getMessage("pref_autodownload_act_text"));</script></label>
        <br>
        <div id="autodown-val-checkbox" style="margin-top:10px;margin-left:20px;"><script>document.write(chrome.i18n.getMessage("pref_autodownload_after_text"));</script>
        <input id="autodown-value" type="text" onblur="if(this.value =='') this.value = '0';" onchange="settingChangedEvt();" onkeyup="this.value=this.value.replace(/[^0-9]/g, '');" maxlength="2" size="2" style="text-align:right;">&nbsp;&nbsp;<script>document.write(chrome.i18n.getMessage("pref_autodownload_time_text"));</script>
        </div>
      </div>
    </div>

   

    <div class="tabContent" id="about">
      <div id="securite" class="filecontainer" style="-webkit-user-select:auto;">
        <br />
        <center>
            <img src="logo76.png"><br />
            <font size="4" style="font-weight: bold;">FilePremier - Plugin</font><br />
            <script>document.write(chrome.i18n.getMessage("pref_version_text")+ "&nbsp;" + chrome.i18n.getMessage("pref_version_num_text"));</script><br />
            <b><script>document.write(chrome.i18n.getMessage("pref_website_text"));</script></b><br />
            <a href="http://www.filepremier.com" style="text-decoration:none;">http://www.filepremier.com</a>
        </center>
        <br />
      </div>
    </div>
    <div align="right" class="buttonfooter">
        <input id="file-save-button" value="OK" type="button" disabled="true" onclick='saveConfig();'>
        <input id="file-cancel-button" value="Cancel" type="button" onclick='loadConfig();'>
    </div>

  </body>
</html>
